//构造函数
function Router() {
    this.routes = {};
    this.currentUrl = '';
    this.defaultPage = '';
    this.contentList="";
};
Router.prototype.route = function(path, callback) {
    this.routes[path] = callback || function(){};//给不同的hash设置不同的回调函数
};
Router.prototype.refresh = function() {
    if(!location.hash.slice(1)){
    	var stateObject = {};
		var title = "Wow Title";
		var newUrl = location.origin+location.pathname+'#'+this.defaultPage;
		history.pushState(stateObject,title,newUrl);
    }
    this.currentUrl = location.hash.slice(1) || '/';//如果存在hash值则获取到，否则设置hash值为/
    // console.log(this.currentUrl);
    try{
    	this.routes[this.currentUrl]();//根据当前的hash值来调用相对应的回调函数
    }catch(e){
    	//TODO handle the exception
    	var stateObject = {};
		var title = "Wow Title";
		var newUrl = location.origin+location.pathname+'#'+this.defaultPage;
		history.pushState(stateObject,title,newUrl);
    	this.routes[this.defaultPage]();
    }
};
Router.prototype.init = function() {
    window.addEventListener('load', this.refresh.bind(this), false);
    window.addEventListener('hashchange', this.refresh.bind(this), false);
};
Router.prototype.go = function(path,content) {
	loadHide();
	if(content){
		this.contentList=JSON.parse(content);
		var stateObject = {};
		var title = "Wow Title";
		var newUrl = location.origin+location.pathname+'#'+path;
		history.pushState(stateObject,title,newUrl);
		Router.refresh();
	}else{
		var stateObject = {};
		var title = "Wow Title";
		var newUrl = location.origin+location.pathname+'#'+path;
		history.pushState(stateObject,title,newUrl);
		Router.refresh();
	}
  	
};
Router.prototype.default = function(path){
	this.defaultPage = path;
	Router.refresh();
};
//给window对象挂载属性
window.Router = new Router();
window.Router.init();